package com.zhuhanpeng.cloud.spring.util;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.Download;
import com.qcloud.cos.transfer.TransferManager;
import com.zhuhanpeng.cloud.file.entity.COSEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class UserFileUtil {
    private static Logger logger = LogManager.getLogger(UserFileUtil.class);

    /**
     * 阿里云上传文件到oss
     *
     * @param aliyunOSS
     * @param fileName
     * @param file
     * @return
     */
    public static String aliyunUploadUserAvatar(COSEntity aliyunOSS, String fileName, MultipartFile file) {
        try {
            // 创建OSS实例。
            OSS ossClient = new OSSClientBuilder().build(aliyunOSS.getRegion(), aliyunOSS.getSecretId(), aliyunOSS.getSecretKey());
            //获取上传文件输入流
            InputStream inputStream = file.getInputStream();
            //调用oss方法实现上传
            //参数一：Bucket名称  参数二：上传到oss文件路径和文件名称  比如 /aa/bb/1.jpg 或者直接使用文件名称  参数三：上传文件的流
            ossClient.putObject(aliyunOSS.getBucket(), fileName, inputStream);

            //关闭OSSClient
            ossClient.shutdown();
            //把上传之后的文件路径返回
            //需要把上传到阿里云路径返回
            //https://hcqianqiu.oss-cn-shanghai.aliyuncs.com/1566480735359.jpg
            String url = " https://" + aliyunOSS.getBucket() + "." + aliyunOSS.getRegion() + "/" + fileName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String tencentUpdateFile(COSEntity tencentCOS, String fileName, MultipartFile file) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tencentCOS.getSecretId(), tencentCOS.getSecretKey());
        // 2 设置bucket的区域, COS地域在上面有提到
        ClientConfig clientConfig = new ClientConfig(new Region(tencentCOS.getRegion()));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        String type = file.getContentType(); //获取文件格式
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(type);
        String url = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(tencentCOS.getBucket(), fileName, file.getInputStream(), objectMetadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            url = "https://" + tencentCOS.getBucket() + ".cos." + tencentCOS.getRegion() + ".myqcloud.com/" + fileName;
        } catch (CosClientException | IOException e) {
            e.printStackTrace();
        } finally {
            cosClient.shutdown();
        }
        return url;
    }

    public static void tencentDeleteFile(COSEntity tencentCOS, String fileName) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tencentCOS.getSecretId(), tencentCOS.getSecretKey());
        // 2 设置bucket的区域, COS地域在上面有提到
        ClientConfig clientConfig = new ClientConfig(new Region(tencentCOS.getRegion()));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        try {
            cosClient.deleteObject(tencentCOS.getBucket(), fileName);
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }

    public static void tencentDownloadFile(COSEntity tencentCOS, String fileName) {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tencentCOS.getSecretId(), tencentCOS.getSecretKey());
        // 2 设置bucket的区域, COS地域在上面有提到
        ClientConfig clientConfig = new ClientConfig(new Region(tencentCOS.getRegion()));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        try {
            cosClient.getObject(tencentCOS.getBucket(), fileName);
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }
}
